package com.yc.chatroom.ui.client.controller;

import java.net.URL;
import java.util.Date;
import java.util.ResourceBundle;

import com.yc.chatroom.common.Constant;
import com.yc.chatroom.common.dto.LoginDto;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.base.Strings;
import com.yc.chatroom.client.ClientManager;
import com.yc.chatroom.common.dto.MsgDto;
import com.yc.chatroom.common.dto.SendMsgDto;
import com.yc.chatroom.common.dto.ShowMsgDto;
import com.yc.chatroom.ui.client.fx.StageManager;
import com.yc.chatroom.ui.client.utils.SpringUtils;

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.util.Callback;

@Component
public class ChatRoomController extends BaseController implements Initializable {

	public static ChatRoomController ctrl;
	
	@FXML
	private ListView<ShowMsgDto> ListView_msgItem;

	//消息列表数据
	private ObservableList<ShowMsgDto> ListView_msgItem_value = FXCollections.observableArrayList();
	
	@FXML
	private TextArea TextArea_writeArea;
	
	@FXML
	private Button Button_submit;

	@FXML
	private Label Label_tips;
	
	private StageManager stageManager;
	
	@Autowired
	private ClientManager clientManager;
	
	
	/**
	 * 用于移动窗口
	 */
	private double xOffSet = 0;
	private double yOffSet = 0;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		ctrl = this;
		stageManager = SpringUtils.getBean(StageManager.class);

		ListView_msgItem.setItems(ListView_msgItem_value);
		ListView_msgItem.setCellFactory((ListView<ShowMsgDto> l) -> new MsgItem());

		new Thread(() -> {
			//不断从消息队列中读出消息，并显示
			while(true) {
				try {
					ShowMsgDto smd = clientManager.MSG_QUEUE.take();
					System.out.println("client received:" + smd.getMessage());

					Platform.runLater(() -> {
						ListView_msgItem_value.add(smd);
					});

				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}


		}).start();

	}
	
	/**
	 * 发送按钮事件
	 */
	@FXML
	public void SubmitButtonAction() {
		System.out.println("---------Submit");
		//获取输入框的内容
		String text = TextArea_writeArea.getText().trim();
		if (text.length() > 200) {
			Label_tips.setText("最多发送200个字符");
			return;
		} else {
			Label_tips.setText("");
		}
		if (!Strings.isNullOrEmpty(text)) {
			//发送
			System.out.println("------text---" + text);
			SendMsgDto msg = new SendMsgDto(text, new Date().getTime());

			clientManager.sendMsg(msg);
			//清空输入框
			TextArea_writeArea.clear();
		} else {
			Label_tips.setText("不能发送空内容");
		}

		
	}
	
	class MsgItem extends ListCell<ShowMsgDto> {

		@Override
		protected void updateItem(ShowMsgDto item, boolean empty) {
			super.updateItem(item, empty);//没有 这一行 添加数据会有问题
			if (!empty) {
				//居左放一个imageView，把头像显示出来
				HBox hb = new HBox();
				ImageView iv = new ImageView("file:////Users/yc/gitsource/chatroom/chatroom-ui-client/src/main/resources/imgs/head.png");
				iv.setFitWidth(50);
				iv.setFitHeight(50);
				
				//右边放一行 单行text
				VBox vb = new VBox();
				vb.setMaxWidth(600);
				Label lb = new Label(item.getUserName());
				Label lb2 = new Label(item.getMessage());
				lb2.setWrapText(true);//消息换行显示
				vb.getChildren().addAll(lb, lb2);
				hb.getChildren().addAll(iv, vb);
				
				this.setGraphic(hb);
			} else {
				this.setText(null);
				this.setGraphic(null);
			}
			
		}
		
	}
	
	
	
//	/**
//	 * 鼠标事件
//	 */
//	@FXML
//	public void mousePressedEvent(MouseEvent event) {
//		xOffSet = event.getSceneX();
//	    yOffSet = event.getSceneY();
//	}
//	
//	/**
//	 * 鼠标事件
//	 */
//	@FXML
//	public void mouseDraggedEvent(MouseEvent event) {
//		stageManager.primaryStage.setX(event.getScreenX() - xOffSet);
//		stageManager.primaryStage.setY(event.getScreenY() - yOffSet);
//	}

}
